# This Python file uses the following encoding: utf-8
import os
from pathlib import Path

from PySide2 import QtWidgets,QtCore
from PySide2.QtWidgets import QApplication,QMessageBox,QWidget
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
from catchObj.ThreadRun import *

from catchObj.CtrlThread import CtrlThread
from MySignals import global_ms
from db import ObjLink
import catchObj.Stocks


class CatchObj(QWidget):

    def __init__(self,ui):
        super(CatchObj,self).__init__()
        self.ui = ui
        self.testOk = []
        self.ctrlThread = CtrlThread()
        self.ctrlThread.setDaemon(True)
        self.stocksNum = len(self.ctrlThread.stocks)
        #初始化页面,会调用控制线程配置
        self.initPage()
        #配置完成,启动控制线程,这里顺序不能错
        CtrlThread.Tstart()


    def initPage(self):
        self.ui.stocksNum.setText(str(len(Stocks.stocks)))
        self.ui.updateAllStocks.clicked.connect(self.updateAllStocks)
        self.ui.updateAllDaily.clicked.connect(self.updateAllDaily)
        self.ui.catchButton.clicked.connect(self.catchRun)
        self.ui.testButton.clicked.connect(self.testRun)
        self.ui.zhengliButton.clicked.connect(self.zhengliRun)
        self.ui.toMongoButton.clicked.connect(self.toMongoRun)
        self.ui.clearFsButton.clicked.connect(self.clearFsRun)
        self.ui.threadsNum.currentIndexChanged.connect(self.configChanged)

        self.ui.tpStart1.editingFinished.connect(self.configChanged)
        self.ui.tpStart2.editingFinished.connect(self.configChanged)
        self.ui.tpEnd1.editingFinished.connect(self.configChanged)
        self.ui.tpEnd2.editingFinished.connect(self.configChanged)
        self.ui.tpDelCatch.editingFinished.connect(self.configChanged)
        self.ui.tpTest.editingFinished.connect(self.configChanged)
        self.ui.tpToMongo.editingFinished.connect(self.configChanged)

        global_ms.setCatchMsg.connect(self.updateCatch)
        global_ms.setCtrlMsg.connect(self.updateCtrl)
        global_ms.setCtrlStatus.connect(self.updateCtrlStatus)
        global_ms.setTest.connect(self.updateTestResult)
        global_ms.setNowTime.connect(self.updateNowTime)
        global_ms.ceshi.connect(self.ceshi)

        #页面配置更改都需要调用这个函数,更新配置文件,重新计算相关变量
        self.configChanged()

    def ceshi(self,dict):
        if dict['action'] == "catchbutton":
            print("完成")

    def updateCtrlStatus(self, str):
        self.ui.ctrlStatus.setText(str)

    def updateCtrl(self, str):
        self.ui.ctrlMsg.appendPlainText(str)

    def updateCatch(self, str):
        self.ui.catchMsg.appendPlainText(str)

    def updateTestOk(self, str):
        print("今日开盘")

    def updateNowTime(self, str):
        self.ui.nowTime.setText(str)

    def updateTestResult(self, str):
        tr = self.ui.testResult
        if str:
            tr.setText("开盘")
        else:
            tr.setText("闭盘")

    def configChanged(self):
        self.timeInfo = {
            "tpStart1": self.ui.tpStart1.time(),
            "tpEnd1": self.ui.tpEnd1.time(),
            "tpStart2": self.ui.tpStart2.time(),
            "tpEnd2": self.ui.tpEnd2.time(),
            "tpDelCatch": self.ui.tpDelCatch.time(),
            "tpTest": self.ui.tpTest.time(),
            "tpToMongo": self.ui.tpToMongo.time(),
            "tpDelFs":self.ui.tpDelFs.time()
        }

        self.threadsNum = int(self.ui.threadsNum.currentText())
        self.pages = self.ctrlThread.setConfig(self.threadsNum, self.timeInfo)
        self.ui.pages.setText(str(self.pages))

    def testRun(self):
        self.ctrlThread.temporaryTaskName = "test"

    def toMongoRun(self):
        self.ctrlThread.temporaryTaskName = "toMongo"

    def zhengliRun(self):
        reply = QMessageBox.warning(self, "警告-删除抓取数据", "本操作会影响redis中的数据,请谨慎操作!", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        if reply == QMessageBox.Yes:
            self.ctrlThread.temporaryTaskName = "delCatch"
        else:
            print("用户取消")

    def clearFsRun(self):
        reply = QMessageBox.warning(self, "警告-删除分时数据", "本操作会影响redis中的数据,请谨慎操作!", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        if reply == QMessageBox.Yes:
            self.ctrlThread.temporaryTaskName = "delFs"
        else:
            print("用户取消")

    def updateAllStocks(self):
        self.ctrlThread.updateAllStocks()
        self.configChanged()

    def updateAllDaily(self):
        self.ctrlThread.temporaryTaskName = "updateAllDialy"

    #线程开启
    def catchRun(self):
        if self.ui.catchButton.text() == "停止":
            #停止
            self.ui.catchButton.setStyleSheet("background-color:rgb(22, 176, 47);")
            self.ui.catchButton.setText("开始抓取")
            self.ui.catchStatus.setText("")
            self.ctrlThread.pause()
        else:
            #开始
            self.ui.catchButton.setStyleSheet("background-color:red;")
            self.ui.catchButton.setText("停止")
            self.configChanged()
            self.ctrlThread.resume()
